package com.google.code.chaotic.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

//

@PersistenceCapable
public class HabitInService implements Serializable {

	// private static final String SERVICE_TAG = "xxxSERVICExxx";
	// private static final String HABIT_TAG = "xxxHABITxxx";

	/**
	 * @uml.property name="service"
	 * @uml.associationEnd 
	 *                     inverse="habits:com.google.code.chaotic.model.CommutingService"
	 */
	@Persistent
	private CommutingService service;

	/**
	 * @uml.property name="serviceId"
	 */
	@Persistent
	private String serviceId;

	/**
	 * @uml.property name="habit"
	 * @uml.associationEnd aggregation="shared"
	 *                     inverse="habitInService:com.google.code.chaotic.model.CommutingHabit"
	 */
	@NotPersistent
	private CommutingHabit habit;

	/**
	 * @uml.property name="habitId"
	 */
	@Persistent
	private String habitId;

	/**
	 * @uml.property name="cancellingCode"
	 */
	@Persistent
	private String cancellingCode;

	/**
	 * @uml.property name="cancellationTime"
	 */
	@Persistent
	private Date cancellationTime;

	//

	// @PrimaryKey
	// public String getId() {
	// return new StringBuilder() //
	// .append(SERVICE_TAG) //
	// .append(getServiceId())//
	// .append(HABIT_TAG) //
	// .append(getHabitId()) //
	// .toString();
	// }
	//
	// public void setId(String id) {
	// // throw new UnsupportedOperationException();
	// if (id == null) {
	// setServiceId(null);
	// setHabitId(null);
	//
	// } else {
	// int index1 = SERVICE_TAG.length();
	// int index2 = id.indexOf(HABIT_TAG);
	// setServiceId(id.substring(index1, index2));
	// setHabitId(id.substring(index2 + HABIT_TAG.length()));
	// }
	// }

	/** @uml.property name="gaeId" */
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String gaeId;

	public void setGaeId(String gaeId) {
		this.gaeId = gaeId;
	}

	public String getGaeId() {
		return gaeId;
	}

	//

	/** @uml.property name="service" */
	public CommutingService getService() {
		return service;
	}

	/** @uml.property name="service" */
	public void setService(CommutingService service) {
		this.service = service;
		serviceId = (service != null ? service.getId() : null);
	}

	/**
	 * @uml.property name="serviceId"
	 */
	public String getServiceId() {
		return service != null ? service.getId() : serviceId;
	}

	/** @uml.property name="serviceId" */
	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
		if (service != null && !service.getId().equals(serviceId))
			service = null;
	}

	/** @uml.property name="habit" */
	public CommutingHabit getHabit() {
		return habit;
	}

	/**
	 * @uml.property name="habit"
	 */
	public void setHabit(CommutingHabit habit) {
		this.habit = habit;
	}

	/**
	 * @uml.property name="habitId"
	 */
	public String getHabitId() {
		return habit != null ? habit.getId() : habitId;
	}

	/** @uml.property name="habitId" */
	public void setHabitId(String habitId) {
		this.habitId = habitId;
		if (habit != null && !habit.getId().equals(habitId))
			habit = null;
	}

	/**
	 * @uml.property name="cancellingCode"
	 */
	public void setCancellingCode(String cancellingCode) {
		this.cancellingCode = cancellingCode;
	}

	/**
	 * @uml.property name="cancellingCode"
	 */
	public String getCancellingCode() {
		return cancellingCode;
	}

	/**
	 * @uml.property name="cancellationTime"
	 */
	public void setCancellationTime(Date cancellationTime) {
		this.cancellationTime = cancellationTime;
	}

	/**
	 * @uml.property name="cancellationTime"
	 */
	public Date getCancellationTime() {
		return cancellationTime;
	}

	@Override
	public String toString() {
		return String.format(//
				"(serviceId: %1$s, habitId: %2$s)", //
				getServiceId(), getHabitId());
	}
}
